package handler

import (
	"image-text-mgr/init"
	"image-text-mgr/model"
	"image-text-mgr/pkg"

	"github.com/gin-gonic/gin"
)

func UserRegister(c *gin.Context) {

	var Username = c.PostForm("username")
	var Password = c.PostForm("password")

	if Username == "" || Password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名和密码不能为空",
		})
		return
	}
	var user model.User
	if user.ID != 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户已存在",
		})
		return
	}

	user.Username = Username
	user.Password = pkg.MD5(Password)
	err := init.DB.Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
	return

}
